package org.i1510.common.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.i1510.common.bean.ConfigBean;
import org.i1510.common.utils.ConfPropertiesUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.common.utils.web.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;






@Controller
@RequestMapping(value = "/common/config")
public class ConfigController {
	private static final Logger logger = LoggerFactory.getLogger(ConfigController.class);
	
	private ResultMapper mapper;
	
	/**
	*@Author Rambo
	*@Desc：保存对象
	*@param configBean
	*@param errors
	*@return TODO
	*@Version  ConfigController.java,v 1.1 2013-11-15 下午4:20:56 
	*/
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,@Valid @ModelAttribute("configBean") ConfigBean configBean,BindingResult errors) {
		ResultMapper mapperRtn = ResultMapper.generateFailure();
		if(errors.hasErrors()){			
			mapper = WebUtils.getMapper4Errors(errors);
		}else{
			String path = "/config/common/conf.properties";
			ConfPropertiesUtils pUtils = new ConfPropertiesUtils();
			Map<String,String> map = new HashMap<String,String>();
			map.put("web.name", configBean.getWebName());
			map.put("web.key", configBean.getWebKey());
			map.put("web.desc", configBean.getWebDesc());
			map.put("web.icp", configBean.getWebIcp());
			map.put("web.sensitive.key", configBean.getWebSensitiveKey());
			map.put("web.nick.key", configBean.getWebNickKey());
			map.put("notice.success", configBean.getNoticeSuccess());
			map.put("notice.accusation.success", configBean.getNoticeAccusationSuccess());
			map.put("notice.accusation.fail", configBean.getNoticeAccusationFail());
			map.put("notice.sumbit.success", configBean.getNoticeSumbitSuccess());
			map.put("notice.sumbit.fail", configBean.getNoticeSumbitFail());
			map.put("email.beta.audit.success", configBean.getEmailBetaAuditSuccess());
			map.put("email.find.pw", configBean.getEmailFindPw());
			
			pUtils.updatePro("path",map, UserUtils.getSessionUserBean(request).getSysUser().getName());
			mapperRtn = ResultMapper.generateSuccess();
			mapperRtn.appendResult("baseRtn", map);
		}
		
		return mapper.toJson();
	}
	

	/**
	*@Author Rambo
	*@Desc：读取对象
	*@param errors
	*@return TODO
	*@Version  ConfigController.java,v 1.1 2013-12-3 下午3:45:30 
	*/
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	@ResponseBody
	public ModelAndView view(HttpServletRequest request,@Valid @ModelAttribute("fdId") String fdId,BindingResult errors) {
		if(errors.hasErrors()){			
			ModelAndView mv = new ModelAndView("wuyo/show/welcom");
			mv.addObject("result", "failure");
			mv.addObject("message", "请求失败!");
			return mv;
		}else{
			String path = "/config/common/conf.properties";
			ConfPropertiesUtils pUtils = new ConfPropertiesUtils();			
			Properties pro = pUtils.loadPro(path);
			Map map = new HashMap();
			map.put("webName", pro.getProperty("web.name"));
			map.put("webKey", pro.getProperty("web.key"));
			map.put("webDesc", pro.getProperty("web.desc"));
			map.put("webIcp", pro.getProperty("web.icp"));
			map.put("webSensitiveKey", pro.getProperty("web.sensitive.key"));
			map.put("webNickKey", pro.getProperty("web.nick.key"));
			map.put("noticeSuccess", pro.getProperty("notice.success"));
			map.put("noticeAccusationSuccess", pro.getProperty("notice.accusation.success"));
			map.put("noticeAccusationFail", pro.getProperty("notice.accusation.fail"));
			map.put("noticeSumbitSuccess", pro.getProperty("notice.sumbit.success"));
			map.put("noticeSumbitFail", pro.getProperty("notice.sumbit.fail"));
			map.put("emailBetaAuditSuccess", pro.getProperty("email.beta.audit.success"));
			map.put("emailFindPw", pro.getProperty("email.find.pw"));
			
			
			ModelAndView mv = new ModelAndView("wuyo/admin/config/base/index");
			mv.addObject("result", "success");
			return mv;
		}		
       
        
        
	}
}